﻿package src {
	
	import flash.display.Graphics;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	/**
	 * Class to represent the navigation tab with miniview and zoom in-out buttons
	 * @author Indrek Tamming
	 */
	public class ViewTab extends Tab {
		
		/**
		 * Callback to the View - base class
		 */
		private var cb:Viewer;
		
		/**
		 * Constructor
		 * @param	cb	-	 Viewer callback
		 */
		public function ViewTab(cb:Viewer):void {
			this.cb = cb;
		}
		
		/**
		 * Draw the miniview and zoom level buttons
		 */
		public function draw():void {
			while (this.numChildren > 0) this.removeChildAt(0);
			
			var miniView:Sprite = new Sprite();
			miniView.x = -5;
			
			var miniViewWidth:Number = 120;
			var horizUnit:Number = miniViewWidth / cb.seqData.getSeqRowLength();
			var miniViewHeight:Number = 100;
			var vertUnit:Number = miniViewHeight / cb.seqData.getNumberOfSeqRows();
			
			var g:Graphics = miniView.graphics;
			g.clear();
			g.lineStyle(2, 0);
			g.drawRect(0, 0, miniViewWidth, miniViewHeight);
			
			cb.miniViewRect = new MovieClip();
			cb.miniViewRect.addEventListener(MouseEvent.MOUSE_DOWN, dragMiniView);
			cb.miniViewRect.buttonMode = true;
			g = cb.miniViewRect.graphics;
			g.clear();
			g.lineStyle(1, 0);
			g.beginFill(0, 0.1);
			var miniViewRectWidth:Number = cb.getNumberOfLettersInRow(cb.zoomLevel) * horizUnit;
			if (miniViewRectWidth > miniViewWidth) {
				// something here?
				miniViewRectWidth = miniViewWidth;
			}
			
			var miniViewRectHeight:Number = cb.getNumberOfRowsOnView(cb.zoomLevel) > cb.seqData.getNumberOfSeqRows() ? miniViewHeight : cb.getNumberOfRowsOnView(cb.zoomLevel) * vertUnit;
			if (miniViewRectHeight > miniViewHeight) {
				// something here?
				miniViewRectHeight = miniViewHeight;
			}
			g.drawRect(0, 0, miniViewRectWidth, miniViewRectHeight);
			g.endFill();
			if(miniViewRectWidth < 10 || miniViewRectHeight < 10) {
				g.lineStyle();
				g.beginFill(0x0000FF, 0.1);
				g.drawCircle(miniViewRectWidth/2, miniViewRectHeight/2, 8);
				g.endFill();
			}
			cb.miniViewRect.x = cb.globalLeftSymbolIndex * horizUnit;
			if (cb.miniViewRect.x < 0) cb.miniViewRect.x = 0;
			cb.miniViewRect.y = cb.globalUpperRowIndex * vertUnit;
			if (cb.miniViewRect.y < 0) cb.miniViewRect.y = 0;
			cb.miniViewRect.maxX = miniViewWidth - miniViewRectWidth + 1; // +1 for border correction
			cb.miniViewRect.maxY = miniViewHeight - miniViewRectHeight + 1;
			cb.miniViewRect.horizUnit = horizUnit;
			cb.miniViewRect.vertUnit = vertUnit;
			
			miniView.addChild(cb.miniViewRect);
			this.addChild(miniView);
			
			
			var zoomLevel1Btn:MovieClip = new ZoomLevelBtn();
			zoomLevel1Btn.gotoAndStop(cb.zoomLevel == 1 ? 3 : 1);
			zoomLevel1Btn.x = 0;
			zoomLevel1Btn.y = 160;
			if(cb.zoomLevel != 1) {
				zoomLevel1Btn.addEventListener(MouseEvent.MOUSE_OVER, mouseOverZoomLvlBtn);
				zoomLevel1Btn.addEventListener(MouseEvent.MOUSE_OUT, mouseOutZoomLvlBtn);
				zoomLevel1Btn.addEventListener(MouseEvent.CLICK, lvl1BtnClick);
			}
			zoomLevel1Btn.buttonMode = cb.zoomLevel == 1 ? false : true;
			zoomLevel1Btn.width = zoomLevel1Btn.height = 10;
			addChild(zoomLevel1Btn);
			
			var zoomLevel2Btn:MovieClip = new ZoomLevelBtn();
			zoomLevel2Btn.gotoAndStop(cb.zoomLevel == 2 ? 3 : 1);
			zoomLevel2Btn.x = 15;
			zoomLevel2Btn.y = 150;
			if(cb.zoomLevel != 2) {
				zoomLevel2Btn.addEventListener(MouseEvent.MOUSE_OVER, mouseOverZoomLvlBtn);
				zoomLevel2Btn.addEventListener(MouseEvent.MOUSE_OUT, mouseOutZoomLvlBtn);
				zoomLevel2Btn.addEventListener(MouseEvent.CLICK, lvl2BtnClick);
			}
			zoomLevel2Btn.buttonMode = cb.zoomLevel == 2 ? false : true;
			zoomLevel2Btn.width = zoomLevel2Btn.height = 20;
			addChild(zoomLevel2Btn);
			
			var zoomLevel3Btn:MovieClip = new ZoomLevelBtn();
			zoomLevel3Btn.gotoAndStop(cb.zoomLevel == 3 ? 3 : 1);
			zoomLevel3Btn.x = 40;
			zoomLevel3Btn.y = 140;
			if(cb.zoomLevel != 3) {
				zoomLevel3Btn.addEventListener(MouseEvent.MOUSE_OVER, mouseOverZoomLvlBtn);
				zoomLevel3Btn.addEventListener(MouseEvent.MOUSE_OUT, mouseOutZoomLvlBtn);
				zoomLevel3Btn.addEventListener(MouseEvent.CLICK, lvl3BtnClick);
			}
			zoomLevel3Btn.buttonMode = cb.zoomLevel == 3 ? false : true;
			zoomLevel3Btn.width = zoomLevel3Btn.height = 30;
			addChild(zoomLevel3Btn);
			
			var zoomLevel4Btn:MovieClip = new ZoomLevelBtn();
			zoomLevel4Btn.gotoAndStop(cb.zoomLevel == 4 ? 3 : 1);
			zoomLevel4Btn.x = 75;
			zoomLevel4Btn.y = 130;
			if(cb.zoomLevel != 4) {
				zoomLevel4Btn.addEventListener(MouseEvent.MOUSE_OVER, mouseOverZoomLvlBtn);
				zoomLevel4Btn.addEventListener(MouseEvent.MOUSE_OUT, mouseOutZoomLvlBtn);
				zoomLevel4Btn.addEventListener(MouseEvent.CLICK, lvl4BtnClick);
			}
			zoomLevel4Btn.buttonMode = cb.zoomLevel == 4 ? false : true;
			zoomLevel4Btn.width = zoomLevel4Btn.height = 40;
			addChild(zoomLevel4Btn);
			
			var zoomTextField:TextField = getFormattedTextField("ZOOM LEVEL = " + cb.zoomLevel, true);
			zoomTextField.y = 170;
			zoomTextField.x = 10;
			this.addChild(zoomTextField);
			
			// navigation arrows?
			
		}
		
		/**
		 * Zoom level 1 button clicked
		 * @param	e
		 */
		private function lvl1BtnClick(e:MouseEvent):void {
			cb.oldZoomLevel = cb.zoomLevel;
			cb.zoomLevel = 1;
			if (cb.oldZoomLevel > cb.zoomLevel) {
				cb.initZoomInRect();
			}
			else cb.zoomOut();
		}
		
		/**
		 * Zoom level 2 button clicked
		 * @param	e
		 */
		private function lvl2BtnClick(e:MouseEvent):void {
			cb.oldZoomLevel = cb.zoomLevel;
			cb.zoomLevel = 2;
			if (cb.oldZoomLevel > cb.zoomLevel) {
				cb.initZoomInRect();
			}
			else cb.zoomOut();
		}
		
		/**
		 * Zoom level 3 button clicked
		 * @param	e
		 */
		private function lvl3BtnClick(e:MouseEvent):void {
			cb.oldZoomLevel = cb.zoomLevel;
			cb.zoomLevel = 3;
			if (cb.oldZoomLevel > cb.zoomLevel) {
				cb.initZoomInRect();
			}
			else cb.zoomOut();
		}
		
		/**
		 * Zoom level 4 button clicked
		 * @param	e
		 */
		private function lvl4BtnClick(e:MouseEvent):void {
			cb.oldZoomLevel = cb.zoomLevel;
			cb.zoomLevel = 4;
			if (cb.oldZoomLevel > cb.zoomLevel) {
				cb.initZoomInRect();
			}
			else cb.zoomOut();
		}
		
		/**
		 * Mouse out zoom level button
		 * @param	e
		 */
		private function mouseOutZoomLvlBtn(e:MouseEvent):void {
			e.target.gotoAndStop(1);
		}
		
		/**
		 * Mouse over zoom level button
		 * @param	e
		 */
		private function mouseOverZoomLvlBtn(e:MouseEvent):void {
			e.target.gotoAndStop(2);
		}
		
		/**
		 * Called when miniview is dragged
		 * @param	e
		 */
		private function dragMiniView(e:MouseEvent):void {
			cb.oldMouseCoordinates.x = cb.mouseX;
			cb.oldMouseCoordinates.y = cb.mouseY;
			cb.miniViewDragging = true;
		}
		
		/**
		 * Get a Textfield with format
		 * @param	text
		 * @param	bold
		 * @return
		 */
		private function getFormattedTextField(text:String, bold:Boolean = false):TextField {
			var tf:TextField = new TextField();
			tf.autoSize = TextFieldAutoSize.LEFT;
			tf.selectable = false;
			
			var tff:TextFormat = new TextFormat();
			tff.bold = bold;
			tff.size = 12;
			tff.font = "Arial";
			tf.defaultTextFormat = tff;
			tf.text = text;
			return tf;
		}
		
	}
	
}